package com.cskaoyan.market.admin.controller;


import com.cskaoyan.market.admin.service.MarketAdService;
import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/ad")
public class AdminAdController{
    @Autowired
    private MarketAdService marketAdService;

    @GetMapping("/list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

//        ServletInputStream inputStream = req.getInputStream();
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        int length = 0;
//        byte[] bytes = new byte[1024];
//        while ((length = inputStream.read(bytes)) != -1) {
//            byteArrayOutputStream.write(bytes, 0, length);
//        }
//        String requsetBody = byteArrayOutputStream.toString("utf-8");
//        String name = JacksonUtil.parseString(requsetBody, "name");
//        String content = JacksonUtil.parseString(requsetBody, "content");
//        String page = JacksonUtil.parseString(requsetBody, "page");
//        String limit = JacksonUtil.parseString(requsetBody, "limit");
        String page = req.getParameter("page");
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");


//    errno: 0, data: {total: 2, pages: 1, limit: 20, page: 1,…}, errmsg: "成功"}
//    data: {total: 2, pages: 1, limit: 20, page: 1,…}
//    limit: 20
//    list: [{id: 2, name: "活动 美食节", link: "",…}, {id: 3, name: "活动 母亲节", link: "",…}]
//            0: {id: 2, name: "活动 美食节", link: "",…}
//            1: {id: 3, name: "活动 母亲节", link: "",…}
//    page: 1
//    pages: 1
//    total: 2
//    errmsg: "成功"
//    errno: 0

        List<MarketAd> marketAds = marketAdService.adSelect(name, content,sort,order, Integer.valueOf(limit), Integer.valueOf(page));

        Object ok = ResponseUtil.okList(marketAds);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    @PostMapping("/create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requsetBody = byteArrayOutputStream.toString("utf-8");
        //需要获取的字段
        // String id,String name,String link,String url,String position,String content,String enable,String updateTime,String deleted
        String content = JacksonUtil.parseString(requsetBody, "content");
        String enabled = JacksonUtil.parseString(requsetBody, "enabled");
        String link = JacksonUtil.parseString(requsetBody, "link");
        String name = JacksonUtil.parseString(requsetBody, "name");
        String position = JacksonUtil.parseString(requsetBody, "position");
        String url = JacksonUtil.parseString(requsetBody, "url");
        MarketAd marketAd = marketAdService.adCreate(content, enabled, link, name, position, url);
        Object ok = ResponseUtil.ok(marketAd);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    @PostMapping("/update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requsetBody = byteArrayOutputStream.toString("utf-8");
        //需要获取的字段
        // String id,String name,String link,String url,String position,String content,String enable,String updateTime,String deleted
        String id = JacksonUtil.parseString(requsetBody, "id");
        String name = JacksonUtil.parseString(requsetBody, "name");
        String link = JacksonUtil.parseString(requsetBody, "link");
        String url = JacksonUtil.parseString(requsetBody, "url");
        String position = JacksonUtil.parseString(requsetBody, "position");
        String content = JacksonUtil.parseString(requsetBody, "content");
        String enable = JacksonUtil.parseString(requsetBody, "enabled");
        String updateTime = JacksonUtil.parseString(requsetBody, "updateTime");
        //在这一行debug ↓
        String deleted = JacksonUtil.parseString(requsetBody, "deleted");

        List<MarketAd> marketAds = marketAdService.adUpdate(id, name, link, url, position, content, enable, updateTime, deleted);
        Object ok=ResponseUtil.ok(marketAds);
        ObjectMapper objectMapper=JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    @PostMapping("/delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requsetBody = byteArrayOutputStream.toString("utf-8");
        String id = JacksonUtil.parseString(requsetBody, "id");
        marketAdService.deleteAd(id);
        Object ok = ResponseUtil.ok();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }
}
